<?php
namespace app\api\lib;

use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use think\Config;

class Alisms
{
    public static function sendCode(string $mobile,int $code):bool
    {
        $param = [
          'code' => $code
        ];
        {
            AlibabaCloud::accessKeyClient(config('alisms.AccessKeyId'),config('alisms.Secret'))
                ->regionId("cn-hangzhou")
                ->asGlobalClient();

            try {
                $result = AlibabaCloud::rpcRequest()
                    ->product('Dysmsapi')
                    ->version('2017-05-25')
                    ->action('SendSms')
                    ->method('POST')
                    ->host('dysmsapi.aliyuncs.com')
                    ->options([
                        'query' => [
                            'PhoneNumbers' => $mobile,
                            'SignName' => config('alisms.SignName'),
                            'TemplateCode' => config('alisms.TemplateCode'),
                            'TemplateParam' => json_encode($param),
//                        'TemplateParam' => json_encode($param)
                        ],
                    ])
                    ->request();
                print_r($result->toArray());
            } catch (ClientException $e) {

                echo $e->getErrorMessage() . PHP_EOL;

            } catch (ServerException $e) {

                echo $e->getErrorMessage() . PHP_EOL;

            }
         return true;
        }
    }
}